package com.atguigu.product.controller;

import com.atguigu.param.ProductCollectParam;
import com.atguigu.param.ProductIdParam;
import com.atguigu.pojo.Product;
import com.atguigu.product.service.ProductService;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:ProductCartController
 * Package:com.atguigu.product.controller
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/22 19:57
 * @Version 1.0
 */
@RestController
@RequestMapping("product")
public class ProductCartController {
    @Autowired
    private ProductService productService;

    @PostMapping("cart/detail")
    public Product cdetail(@RequestBody @Validated ProductIdParam param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return null;
        }

        R detail = productService.detail(param.getProductID());
        Product product=(Product) detail.getData();
        return product;
    }
@PostMapping("cart/list")
    public List<Product> cartList(@RequestBody @Validated ProductCollectParam param ,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return new ArrayList<Product>();
        }

        return productService.cartList(param.getProductIds());
}
}
